/*
 * NewFormMenu.java
 *
 * Created on May 22, 2006, 10:09 AM
 */

/**
 *
 * @author  Timoz
 */
import javax.swing.*;
import java.awt.*;

public class NewFormMenu extends javax.swing.JFrame {
    private ImageIcon formDesign = new ImageIcon(getClass().getResource("Images/NewDesignForm.png"));
    private ImageIcon formWizard = new ImageIcon(getClass().getResource("Images/NewFormWizard.png"));
    private ImageIcon columnarForm = new ImageIcon(getClass().getResource("Images/NewColumnar.png"));
    private ImageIcon tabularForm = new ImageIcon(getClass().getResource("Images/NewTabular.png"));
    private ImageIcon justifiedForm = new ImageIcon(getClass().getResource("Images/NewJustified.png"));
    java.util.Vector availableTables;
    String selectedTable;
    String selectedFormat;
    
    /** Creates new form NewFormMenu */
    public NewFormMenu() {
        initComponents();
        MainWindow.dbCon.setDatabase(MainWindow.selectedDatabase);
        availableTables=MainWindow.dbCon.getTables();
        jComboBox1.setModel(new DefaultComboBoxModel(availableTables));
        jComboBox1.setSelectedItem(null);
        jList1.setSelectedIndex(0);
        selectedFormat="Design View";
        setLocation(250,150);
        setResizable(false);
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jComboBox1 = new javax.swing.JComboBox();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setIconImage(null);
        jList1.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Design View", "Form Wizard", "AutoForm : Columnar", "AutoForm : Tabular", "AutoForm : Justified" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });

        jScrollPane1.setViewportView(jList1);

        jComboBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jComboBox1ActionPerformed(evt);
            }
        });

        jLabel1.setText("Choose the Table from which the");

        jLabel2.setText("Form's Data will come from.");

        jButton1.setText("Cancel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setText("OK");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/NewDesignForm.png")));
        jLabel3.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(0, 0, 0), 2, true));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .add(jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 184, Short.MAX_VALUE)
                    .add(jLabel3))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, layout.createSequentialGroup()
                        .add(jButton2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 72, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 47, Short.MAX_VALUE)
                        .add(jButton1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 73, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jComboBox1, 0, 192, Short.MAX_VALUE)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 192, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 140, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel3))
                .add(23, 23, 23)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING, false)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jLabel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.LEADING, jComboBox1))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(layout.createSequentialGroup()
                        .add(jLabel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 22, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(29, 29, 29))
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButton2)
                            .add(jButton1))
                        .addContainerGap())))
        );
        pack();
    }// </editor-fold>//GEN-END:initComponents
    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        if (selectedFormat.equals("Design View")) {
            java.util.Vector availableTables=MainWindow.dbCon.getTables();
            if (availableTables.isEmpty()==false) {
                if (selectedTable==null) {
                    JOptionPane.showMessageDialog(this,"You Must Select A Table As The Form's Data Source To Continue","Error",JOptionPane.ERROR_MESSAGE);
                } else {
                    Toolkit kit = Toolkit.getDefaultToolkit();
                    Dimension d=kit.getScreenSize();
                    MyFrame myFrame = new MyFrame(d);
                    myFrame.setVisible(true);
                    myFrame.setTableName(selectedTable);
                    dispose();
                }
            } else {
                JOptionPane.showMessageDialog(null,"The selected Database doesn't contain any tables To create a Form from","Error",JOptionPane.ERROR_MESSAGE);
            }
        } else if (selectedFormat.equals("Form Wizard")) {
            FormWizard formWizard = new FormWizard(MainWindow.myForms,selectedTable);
            dispose();
        } else if (selectedFormat.equals("AutoForm : Columnar")) {
            if (selectedTable==null) {
                JOptionPane.showMessageDialog(this,"You Must Select A Table As The Form's Data Source To Continue","Error",JOptionPane.ERROR_MESSAGE);
            } else {
                FormWizard formWizard = new FormWizard(MainWindow.myForms,selectedTable);
                formWizard.selectedLayout="Columnar";
                formWizard.selectedFields=MainWindow.dbCon.getFields(selectedTable);
                formWizard.flagAutoForm=true;
                formWizard.createForm();
                formWizard.dispose();
                dispose();
            }
        } else if (selectedFormat.equals("AutoForm : Tabular")) {
            if (selectedTable==null) {
                JOptionPane.showMessageDialog(this,"You Must Select A Table As The Form's Data Source To Continue","Error",JOptionPane.ERROR_MESSAGE);
            } else {
                FormWizard formWizard = new FormWizard(MainWindow.myForms,selectedTable);
                formWizard.selectedLayout="Tabular";
                formWizard.selectedFields=MainWindow.dbCon.getFields(selectedTable);
                formWizard.flagAutoForm=true;
                formWizard.createForm();
                formWizard.dispose();
                dispose();
            }
        } else if (selectedFormat.equals("AutoForm : Justified")) {
            if (selectedTable==null) {
                JOptionPane.showMessageDialog(this,"You Must Select A Table As The Form's Data Source To Continue","Error",JOptionPane.ERROR_MESSAGE);
            } else {
                FormWizard formWizard = new FormWizard(MainWindow.myForms,selectedTable);
                formWizard.selectedLayout="Justified";
                formWizard.selectedFields=MainWindow.dbCon.getFields(selectedTable);
                formWizard.flagAutoForm=true;
                formWizard.createForm();
                formWizard.dispose();
                dispose();
            }
        }
    }//GEN-LAST:event_jButton2ActionPerformed
    
    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
        selectedFormat=(String)jList1.getSelectedValue();
        if (selectedFormat.equals("Design View")) {
            jLabel3.setIcon(formDesign);
        } else if (selectedFormat.equals("Form Wizard")) {
            jLabel3.setIcon(formWizard);
        } else if (selectedFormat.equals("AutoForm : Columnar")) {
            jLabel3.setIcon(columnarForm);
        } else if (selectedFormat.equals("AutoForm : Tabular")) {
            jLabel3.setIcon(tabularForm);
        } else if (selectedFormat.equals("AutoForm : Justified")) {
            jLabel3.setIcon(justifiedForm);
        }
    }//GEN-LAST:event_jList1ValueChanged
    
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        dispose();
    }//GEN-LAST:event_jButton1ActionPerformed
    
    private void jComboBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jComboBox1ActionPerformed
        selectedTable=(String)jComboBox1.getSelectedItem();
    }//GEN-LAST:event_jComboBox1ActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new NewFormMenu().setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JComboBox jComboBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JList jList1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables
    
}
